package com.atguigu.realtime.gmallpublisher.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.realtime.gmallpublisher.bean.ProductStats;
import com.atguigu.realtime.gmallpublisher.bean.ProvinceStats;
import com.atguigu.realtime.gmallpublisher.bean.VisitorStats;
import com.atguigu.realtime.gmallpublisher.service.KeyWordStatsService;
import com.atguigu.realtime.gmallpublisher.service.ProductStatsService;
import com.atguigu.realtime.gmallpublisher.service.ProvinceStatsService;
import com.atguigu.realtime.gmallpublisher.service.VisitorStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/26 14:43
 */
@RestController
@RequestMapping("/api/sugar")
public class PublisherController {
    
    @Autowired
    ProductStatsService product;
    
    @RequestMapping("/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", product.getGMV(date));
        return JSON.toJSONString(result);
    }
    
    @RequestMapping("/trademark")
    public String getProductStatsByTM(@RequestParam(value = "date", defaultValue = "0") int date,
                                      @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        final List<ProductStats> ProductStatsList = product.getProductStatsByTM(date, limit);
        
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final Map<String, List<Object>> data = new HashMap<>();
        result.put("data", data);
        
        // 构建categories
        final List<Object> categories = new ArrayList<>();
        for (ProductStats ps : ProductStatsList) {
            categories.add(ps.getTm_name());
        }
        data.put("categories", categories);
        
        // 构建 series
        final List<Object> series = new ArrayList<>();
        final HashMap<String, Object> one = new HashMap<>();
        series.add(one);
        
        one.put("name", "尚硅谷品类");
        
        final ArrayList<BigDecimal> oneData = new ArrayList<>();
        one.put("data", oneData);
        for (ProductStats ps : ProductStatsList) {
            oneData.add(ps.getOrder_amount());
        }
        data.put("series", series);
        
        return JSON.toJSONString(result);
        
    }
    
    @RequestMapping("/spu")
    public String getProductStatsBySpu(@RequestParam(value = "date", defaultValue = "0") int date,
                                       @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final List<ProductStats> productStatsList = product.getProductStatsBySpu(date, limit);
        final ArrayList<Map<String, Object>> data = new ArrayList<>();
        result.put("data", data);
        
        for (ProductStats ps : productStatsList) {
            final HashMap<String, Object> map = new HashMap<>();
            map.put("name", ps.getSpu_name());
            map.put("value", ps.getOrder_amount());
            map.put("url", "http://www.atguigu.cn");
            data.add(map);
        }
        
        return JSON.toJSONString(result);
    }
    
    @RequestMapping("/c3")
    public String getProductStatsByC3(@RequestParam(value = "date", defaultValue = "0") int date,
                                      @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final List<ProductStats> productStatsList = product.getProductStatsByC3(date, limit);
        
        final HashMap<String, List<Map<String, Object>>> data = new HashMap<>();
        result.put("data", data);
        
        final ArrayList<Map<String, Object>> columns = new ArrayList<>();
        final ArrayList<Map<String, Object>> rows = new ArrayList<>();
        
        data.put("columns", columns);
        data.put("rows", rows);
        
        final HashMap<String, Object> c1 = new HashMap<>();
        c1.put("name", "3级品类id");
        c1.put("id", "c3_id");
        columns.add(c1);
        final HashMap<String, Object> c2 = new HashMap<>();
        c2.put("name", "3级品类名字");
        c2.put("id", "c3_name");
        columns.add(c2);
        
        final HashMap<String, Object> c3 = new HashMap<>();
        c3.put("name", "订单总额");
        c3.put("id", "order_amount");
        columns.add(c3);
        final HashMap<String, Object> c4 = new HashMap<>();
        c4.put("name", "订单个数");
        c4.put("id", "order_ct");
        columns.add(c4);
        
        for (ProductStats ps : productStatsList) {
            final HashMap<String, Object> row = new HashMap<>();
            rows.add(row);
            row.put("c3_id", ps.getCategory3_id());
            row.put("c3_name", ps.getCategory3_name());
            row.put("order_amount", ps.getOrder_amount());
            row.put("order_ct", ps.getOrder_ct());
            System.out.println(rows);
        }
        
        return JSON.toJSONString(result);
    }
    
    @Autowired
    ProvinceStatsService province;
    
    @RequestMapping("/province")
    public String getProvinceStats(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final HashMap<String, Object> data = new HashMap<>();
        result.put("data", data);
        
        final ArrayList<Map<String, Object>> mapData = new ArrayList<>();
        data.put("mapData", mapData);
        
        final List<ProvinceStats> list = province.getProvinceStats(date);
        
        for (ProvinceStats ps : list) {
            final HashMap<String, Object> map = new HashMap<>();
            mapData.add(map);
            
            map.put("name", ps.getProvince_name());
            map.put("value", ps.getOrder_amount());
        }
        
        return JSON.toJSONString(result);
    }
    
    @Autowired
    VisitorStatsService visitor;
    
    @RequestMapping("/visitor_hr")
    public String getVisitorHr(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final HashMap<String, List<Object>> data = new HashMap<>();
        result.put("data", data);
        
        final ArrayList<Object> categories = new ArrayList<>();
        data.put("categories", categories);
        for (int i = 0; i < 24; i++) {
            categories.add(String.format("%02d", i));  // "0" "1"..."11",
        }
        
        final List<VisitorStats> list = visitor.getVisitorStatsByHour(date); // 10 12
        final VisitorStats[] vsArray = new VisitorStats[24];  // null null  ... vs null vs ....
        for (VisitorStats vs : list) {
            vsArray[vs.getHr()] = vs;
        }
        
        final ArrayList<Object> series = new ArrayList<>();
        data.put("series", series);
        
        final HashMap<String, Object> uvMap = new HashMap<>();
        uvMap.put("name", "uv");
        final ArrayList<Object> uvData = new ArrayList<>();
        uvMap.put("data", uvData);
        
        final HashMap<String, Object> pvMap = new HashMap<>();
        pvMap.put("name", "pv");
        final ArrayList<Object> pvData = new ArrayList<>();
        pvMap.put("data", pvData);
        
        series.add(uvMap);
        series.add(pvMap);
        
        for (int hr = 0; hr < 24; hr++) {
            if (vsArray[hr] != null) {
                uvData.add(vsArray[hr].getUv_ct());
                pvData.add(vsArray[hr].getPv_ct());
            } else {
                uvData.add(0);
                pvData.add(0);
            }
        }
        
        return JSON.toJSONString(result);
    }
    
    @RequestMapping("/visitor_new")
    public String getVisitorNew(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        
        final HashMap<String, Object> data = new HashMap<>();
        result.put("data", data);
        
        final List<VisitorStats> list = visitor.getVisitorStatsByNewFlat(date);
        
        data.put("total", list.size());
        
        final ArrayList<Map<String, String>> columns = new ArrayList<>();
        data.put("columns", columns);
        
        final HashMap<String, String> c1 = new HashMap<>();
        columns.add(c1);
        c1.put("name", "新老用户(1/0)");
        c1.put("id", "is_new");
        
        final HashMap<String, String> c2 = new HashMap<>();
        columns.add(c2);
        c2.put("name", "uv数量");
        c2.put("id", "uv");
        
        final HashMap<String, String> c3 = new HashMap<>();
        columns.add(c3);
        c3.put("name", "pv数量");
        c3.put("id", "pv");
        
        final HashMap<String, String> c4 = new HashMap<>();
        columns.add(c4);
        c4.put("name", "跳出数量");
        c4.put("id", "uj");
        
        final ArrayList<Map<String, Object>> rows = new ArrayList<>();
        data.put("rows", rows);
        for (VisitorStats vs : list) {
            final Map<String, Object> row = new HashMap<>();
            rows.add(row);
            
            row.put("is_new", vs.getIs_new());
            row.put("uv", vs.getUv_ct());
            row.put("pv", vs.getPv_ct());
            row.put("uj", vs.getUj_ct());
        }
        
        
        return JSON.toJSONString(result);
    }
    
    
    @Autowired
    KeyWordStatsService kw;
    @RequestMapping("/kw")
    public String getKw(@RequestParam(value = "date", defaultValue = "0") int date,
                        @RequestParam(value = "limit", defaultValue = "15") int limit) {
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        final Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
    
        final ArrayList<Map<String, Object>> data = new ArrayList<>();
        result.put("data", data);
    
        final List<Map<String, Object>> list = kw.getKeyWordStats(date, limit);
        for (Map<String, Object> kwAndScore : list) {
            final String keyword = kwAndScore.get("keyword").toString();
            final Long score = Long.valueOf(kwAndScore.get("ct").toString());
    
            final HashMap<String, Object> map = new HashMap<>();
            map.put("name", keyword);
            map.put("value", score);
            data.add(map);
    
        }
        return JSON.toJSONString(result);
    }
    
}
